package com.ct;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.StaticLog;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ct.config.mybatisPlus.RequestDataHelper;
import com.ct.mapper.kkrot.UserMapper;
import com.ct.model.entity.kkrot.User;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@SpringBootTest
public class MysqlDemo {

    @Resource
    public UserMapper userMapper;

    //增加
    @Test
    public void IInsert() {
        User user = new User();
        user.setName("小羊");
        user.setAge(3);
        user.setEmail("abc@mp.com");
        int insertId = userMapper.insert(user);
        StaticLog.info("users->{}", insertId);
        // 增加返回主键ID
        StaticLog.info("users->{}", user.getId());
    }

    //删除
    @Test
    public void IDelete() {
        int deleteById = userMapper.deleteById(8);
        StaticLog.info("deleteById->{}", deleteById);

        userMapper.delete(
                new QueryWrapper<User>().lambda().eq(User::getName, "Sandy"));
    }

    //改  更新
    @Test
    public void IUpdate() {
        User user = new User();
        user.setName("李四");
        user.setEmail("ab@c.c");
        int updateById = userMapper.updateById(user);
        StaticLog.info("deleteById->{}", updateById);

        userMapper.update(user, Wrappers.<User>lambdaUpdate().set(User::getAge, 12).eq(User::getId, 2));
        userMapper.update(null, Wrappers.<User>lambdaUpdate().set(User::getEmail, "1").eq(User::getId, 2));
    }

    // 查询
    @Test
    public void ISelect() {
        User user = userMapper.selectById(1L);
        StaticLog.info("user->{}", user);

        User user1 = userMapper.selectOne(new QueryWrapper<User>().lambda().eq(User::getId, 2));
        StaticLog.info("user1->{}", user1);

        //多字段排序
        userMapper.selectList(Wrappers.<User>query().orderByAsc("age"));
        userMapper.selectList(Wrappers.<User>query().orderByAsc("age", "name"));
        userMapper.selectList(Wrappers.<User>query().orderByAsc("age").orderByDesc("name"));

        userMapper.selectMaps(Wrappers.<User>query().orderByAsc("age"));

        //分页
        userMapper.selectMapsPage(new Page<>(1, 5), Wrappers.<User>query().orderByAsc("age"));
    }

    //动态表名
    @Test
    public void dtabliname() {
        try {
            for (int i = 0; i < 4; i++) {
                RequestDataHelper.setRequestData(MapUtil.builder(new HashMap<String, Object>()).put("user", i).build());
                if (StrUtil.isNotBlank(userMapper.hasTable(StrUtil.format("{}_{}", "user", i)))) {
                    List<User> userList = userMapper.selectList(null);
                    StaticLog.info("userList:{}", JSONUtil.toJsonStr(userList));
                }else {
                    StaticLog.error("查询不到动态表->{}", StrUtil.format("{}_{}", "user", i));
                }
            }
        } finally {
            RequestDataHelper.removeRequestData();
        }
    }

}
